/*
* Copyright 2006-2010 Bitlab. All rights reserved.
* Bitlab PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package ua.com.stormlabs.tiny.licenseserver.beans;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * Represent pager info like current page, items count on a page and
 * total page count.
 *
 * @author Bogdan Paranitca
 * @version 1.0, 15 окт. 2010
 */

public class Pager {

    private static final int FIRST_PAGE = 0;

    private final int currentPage;
    private final int itemsPerPage;
    private final int pagesCount;

    /**
     * Create new pager instance.
     * If currentPage is less than 1 then current page set to first page;
     *
     * @param currentPage
     * @param totalItemsCount
     * @param itemsPerPage
     */
    public Pager(int currentPage, long totalItemsCount, int itemsPerPage) {
        this.currentPage = currentPage > 0 ? currentPage : FIRST_PAGE;
        this.itemsPerPage = itemsPerPage;
        this.pagesCount = (int) Math.ceil((double) totalItemsCount / (double) itemsPerPage);
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public int getItemsPerPage() {
        return itemsPerPage;
    }

    public int getPagesCount() {
        return pagesCount;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }
}